//
//  ZDTabBar.swift
//  ZZDWeibo
//
//  Created by zzdan on 16/6/25.
//  Copyright © 2016年 zzdan. All rights reserved.
//

import UIKit

//声明协议
protocol ZDTabBarDelegate: NSObjectProtocol{
    
    func composeButtonClick()
}

class ZDTabBar: UITabBar {
    
// MARK: -  声明代理
    weak var ZDDelegate:ZDTabBarDelegate?

  //设置子控件的frame
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //添加按钮
        setupUI()

        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //子控件的位置
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = SCREENW / 5
        //记录标识
        var index:CGFloat = 0
        
        //遍历得到子控件
        for value in self.subviews{
            
            //如果为UITabBarButton，执行
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.size.width = width
                value.frame.origin.x = index * width
                
                index++
                
                //给撰写按钮留下位置
                if index == 2{
                    
                    index++
                }
    
            }
        }
        
   //设置撰写按钮的位置
        composeButton.center.x = self.frame.width * 0.5
        composeButton.center.y = self.frame.height * 0.5
        
        
    }
    
    
    //设置视图
    func setupUI(){
        addSubview(composeButton)
        
    }
    
    // MARK: -  实现方法
    @objc private func buttonClick(){
        
        ZDDelegate?.composeButtonClick()
        
    }

    
    //撰写按钮
    private lazy var composeButton: UIButton = {
       let button = UIButton()
        
        //设置背景颜色
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        //设置加号图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.sizeToFit()
        
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        

      return button
    
    }()
    
    
}
